package com.vimi8.ebb.auth.configuration;

import com.vimi8.ebb.auth.service.DBClientDetailsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.*;

/**
 * Created by xuybin on 2016/9/9.
 */
public class BaseJwtAccessTokenConverterImpl extends JwtAccessTokenConverter {
    protected Logger logger =  LoggerFactory.getLogger(this.getClass());
    public static final String OPENID = "openid";
    public static final String JOB = "job";
    public static final String CLIENTORGCLASS = "clientOrgClass";
    public static final String EXPIRES_IN = "exp";
    public static final String JTI = "jti";
    public static final String CLIENT_ID = "client_id";
    protected String resourceId;
    @Autowired
    private DBClientDetailsService dbClientDetailsService;
    public BaseJwtAccessTokenConverterImpl(String resourceId){
        this.resourceId=resourceId;
    }

    @Override
    public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
        //expires_in openid jti
        final OAuth2Authentication authentication = super.extractAuthentication(map);
        Set<String> resourceids=authentication.getOAuth2Request().getResourceIds();

        String clientId=(String)map.get(CLIENT_ID);
        logger.info("client_id=="+clientId);
        Long expires_in=(Long)map.get(EXPIRES_IN);

        String jti=(String)map.get(JTI);
        String openid=(String)map.get(OPENID);
        String job=(String)map.get(JOB);
        String coc=(String)map.get(CLIENTORGCLASS);
        final Map<String, String> details = new HashMap<>();
        details.put(OPENID,openid);
        details.put(JOB,job);
        details.put(CLIENTORGCLASS,coc);
        details.put(JTI,jti);
        details.put(EXPIRES_IN,String.valueOf(expires_in));
        authentication.setDetails(details);
        logger.info("authentication details==="+details.toString());
        return authentication;
    }
}
